package run.vue.iblog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import run.vue.iblog.dto.PasswordsDto;
import run.vue.iblog.entity.User;
import run.vue.iblog.mapper.UserMapper;
import run.vue.iblog.service.UserService;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anonymous
 * @since 2020-06-14
 */
@Service
public class UserServiceImpl extends  ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(User user) {
        //个人信息
        String username = user.getUsername();
        String realname = user.getRealname();
        String phone = user.getTelephone();
        String email = user.getEmail();
        String password = user.getPassword();

        // 转义防止XSS攻击
        //   username = HtmlUtils.htmlEscape(username);
        user.setUsername(username);
        // realname = HtmlUtils.htmlEscape(realname);
        user.setRealname(realname);
        //  phone = HtmlUtils.htmlEscape(phone);
        user.setTelephone(phone);
        // email = HtmlUtils.htmlEscape(email);
        user.setEmail(email);
        user.setStatus(1);

        // 输入内容为空
        if (username.equals("") || password.equals("")) {
            return 0;
        }

        // 检测是否为空
        boolean exist = isExist(username);

        if (exist) {
            return 2;
        }
        // 对密码进行加密
        // 默认生成 16 位盐
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        // 加密次数为10次
        int times = 10;
        String encodedPassword = new SimpleHash("md5", password, salt, times).toString();
        user.setSalt(salt);
        user.setPassword(encodedPassword);
        user.setCreated(LocalDateTime.now());
        userMapper.save(user);
        return 1;
    }

    @Override
    public boolean isExist(String username) {
        User user = getByName(username);
        return null != user;
    }

    @Override
    public int getTotalCount() {
        return userMapper.totalCount();
    }

    @Override
    public int getActiveCount() {
        return userMapper.totalActiveCount();
    }

    @Override
    public User getByUsername(String username) {
        return getByName(username);
    }

    // 修改密码
    @Override
    public void updatePassword(PasswordsDto passwordsDto) {
        String modifiedPW = passwordsDto.getRePass();
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        // 加密次数为10次
        int times = 10;
        String encodedPassword = new SimpleHash("md5", modifiedPW, salt, times).toString();
        User user = new User();
        user.setSalt(salt);
        user.setPassword(encodedPassword);
        user.setId(passwordsDto.getUserId());
        System.out.println(user);
        userMapper.modityPW(user);
    }

    // 更新信息
    @Override
    public void updateInfo(User user) {
        userMapper.updateInfo(user);
    }

    private User getByName(String username) {
        return userMapper.getByUserName(username);
    }

}
